const fs = require("fs");
const path = require("path");

const basePath = path.join(__dirname, "../", "public");

function delFile(image_url) {
  const filePath = path.join(basePath, image_url);

  fs.unlink(filePath, (err) => {
    if (err) {
      console.error(`[图片删除失败] ${filePath}:`, err.message);
    } else {
      console.log(`[图片已删除] ${filePath}`);
    }
  });
}

function writeFile(file, folder, filename) {
  const dir = path.join(basePath, 'uploads' ,folder);
  fs.mkdirSync(dir, { recursive: true });

  fs.writeFileSync(path.join(dir, filename), file.buffer);
}

module.exports = {
  delFile,
  writeFile
}
